import { createStore } from 'vuex';

const store = createStore({
  state: {
    books: [],
  },
  mutations: {
    addBook(state, book) {
      state.books.push(book);
    },
    removeBook(state, bookId) {
      state.books = state.books.filter(book => book.id !== bookId);
    },
    setBooks(state, books) {
      state.books = books;
    },
  },
  actions: {
    fetchBooks({ commit }) {
      // Simulate an API call
      const books = [
        { id: 1, title: 'Book One', author: 'Author One' },
        { id: 2, title: 'Book Two', author: 'Author Two' },
      ];
      commit('setBooks', books);
    },
    addBook({ commit }, book) {
      commit('addBook', book);
    },
    removeBook({ commit }, bookId) {
      commit('removeBook', bookId);
    },
  },
  getters: {
    allBooks: (state) => state.books,
  },
});

export default store;